package com.hxl.demo.controller;

import com.hxl.demo.entity.TrainingContents;
import com.hxl.demo.service.Impl.TrainingContentsServiceImpl;
import com.hxl.demo.tools.Common;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin")
public class TrainingContentsController {

    @Resource
    private TrainingContentsServiceImpl trainingContentsService;

    @Resource
    TrainingContents trainingContents;


    @RequestMapping("/getTrainingClassificationDetail")
    public Object getTrainingClassificationDetail(@RequestParam(name = "contentId") Integer contentId){
        trainingContents = trainingContentsService.getTrainingContentsDetail(contentId);

        return Common.returnJsonSuccess("操作成功",400,trainingContents);
    }

    @RequestMapping("/getTrainingContentAdd")
    public Object getTrainingContentAdd(@RequestParam(name = "classId") Integer classId,
                                        @RequestParam(name = "content") String content,
                                        @RequestParam(name = "trainingTime") Integer trainingTime,
                                        @RequestParam(name = "remark",required = false) String remark,
                                        @RequestParam(name = "url") String url,
                                        HttpServletRequest request
    ){
        if(content==null ||content.length()<2 || content.length()>30){
            return  Common.returnJsonFail("文件名称长度在2-30字符之间",400);
        }
        if(url==null || url.length()<2 || url.length()>200){
            return  Common.returnJsonFail("文件地址长度在2-200字符之间",400);
        }
        if(trainingTime==null || trainingTime<1 || trainingTime>10000){
            return  Common.returnJsonFail("培训时长在1-10000分钟之间",400);
        }
        if(remark!=null && remark.length()>100){
            return  Common.returnJsonFail("备注长度在100个字符之间",400);
        }
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userInfo");
        return   trainingContentsService.trainingContentsAdd(classId,content,url,trainingTime,remark,userId);

    }
    @RequestMapping("trainingContentAudit")
    public Object trainingContentAudit(@RequestParam(name = "id") Integer id,@RequestParam(name = "auditStatus") String  auditStatus){
        if(id==null || auditStatus==null){
            return Common.returnJsonFail("必填数据不能为空",400);
        }
        if(!auditStatus.equals("1") && !auditStatus.equals("2")){
            return Common.returnJsonFail("审核状态不正确",400);
        }

        return trainingContentsService.trainingContentAudit(id,auditStatus);


    }

}
